import requests
import pyttsx3

# 百度语言翻译
def baidu():
    url = 'http://fanyi.baidu.com/v2transapi/'
    content = input("中译英请输入1，英译中请输入2\n")
    if content in ['1']:
        content = input("请输入要翻译的内容：")
        data = {
            ' from': 'zh', 'to': 'en', 'query': content,
            'transtype': 'translang',
            'simple_means_flag': '3',
        }
    elif content in ['2']:
        content = input("请输入翻译内容\n")
        data = {
            ' from': 'en', 'to': 'zh', 'query': content,
            'transtype': 'translang',
            'simple_means_flag': '3',
        }

    header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0'}
    response = requests.post(url, data, header)
    head = response.headers
    resout = response.json()['trans_result']['data'][0]['dst']
    print(resout)
    return resout

def yuYin(parm):
    url = "http://fanyi.baidu.com/gettts?lan=en&text=" + parm + "&spd=3&source=web"
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0'}
    mp3 = requests.post(url, header)
    print(mp3)
    return mp3



# 朗读
def requetAloud(str):
    speaker = pyttsx3.init()
    speaker.say(str)
    speaker.runAndWait()


if __name__ == "__main__":
    resout = baidu()
    requetAloud("hello word")